/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Server.Database.Database;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author sarunaskujalis
 */
public class AdminWindowFilters extends javax.swing.JPanel {
    
    private Database db;
    private DefaultListModel listModel;
    
    /**
     * Creates new form AdminWindowFilters
     */
    public AdminWindowFilters(Database db) {
        this.db = db;
        initComponents();
        listModel = new DefaultListModel();
        phrazeList.setModel(listModel);
        loadList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        phrazeList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        addPhrazeBtn = new javax.swing.JButton();
        deletePhrazeBtn = new javax.swing.JButton();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                handleListSelection(evt);
            }
        });

        phrazeList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                handleListSelection(evt);
            }
        });
        jScrollPane1.setViewportView(phrazeList);

        jLabel1.setText("Filtruojamos frazės:");

        addPhrazeBtn.setText("Pridėti frazę");
        addPhrazeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPhrazeBtnActionPerformed(evt);
            }
        });

        deletePhrazeBtn.setText("Pašalinti frazę");
        deletePhrazeBtn.setEnabled(false);
        deletePhrazeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletePhrazeBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(15, 15, 15)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 417, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(addPhrazeBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(deletePhrazeBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(addPhrazeBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(deletePhrazeBtn))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 282, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(19, 19, 19))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addPhrazeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPhrazeBtnActionPerformed
        JTextField phrazeField = new JTextField();
        Object[] message = {
                   "Frazė:", phrazeField
        };

        int option = JOptionPane.showConfirmDialog(null, message, "Frazės pridėjimas", JOptionPane.OK_CANCEL_OPTION);
        if (option == 0) {
            if (phrazeField.getText().length() > 0) {
                try {
                  db.phraseRegister.addPhraze(phrazeField.getText());
                  listModel.addElement(phrazeField.getText());
                 } catch(Exception ex){
                     JOptionPane.showMessageDialog(null, "Toks filtas jau yra.");
                 }
            }
            else {
                JOptionPane.showMessageDialog(null, "Jūs neįvedėte jokio filtro.");
            }
         }
    }//GEN-LAST:event_addPhrazeBtnActionPerformed

    private void deletePhrazeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePhrazeBtnActionPerformed
        String selection = phrazeList.getSelectedValue().toString();
        int userIndex = phrazeList.getSelectedIndex();
        try {
            db.phraseRegister.deletePhraze(selection);
            listModel.remove(userIndex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowBlocks.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_deletePhrazeBtnActionPerformed

    private void handleListSelection(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_handleListSelection
        // TODO add your handling code here:
        if (!phrazeList.isSelectionEmpty()) {
            deletePhrazeBtn.setEnabled(true);
        }
    }//GEN-LAST:event_handleListSelection
    
    private void loadList() {
        try {
            ArrayList<String> list = db.phraseRegister.getAllPhrazes();
            for (String phraze : list) {
                listModel.addElement(phraze);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowBlocks.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPhrazeBtn;
    private javax.swing.JButton deletePhrazeBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList phrazeList;
    // End of variables declaration//GEN-END:variables
}
